package com.kelvin.paopao.easemob;

import android.content.Context;

import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.model.EaseAtMessageHelper;
import com.kelvin.kelvinlib.db.dao.UserInfoDao;
import com.kelvin.paopao.PaoPaoApplication;
import com.hyphenate.easeui.utils.SpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EasemobModel {

    protected Context context = null;
    private UserInfoDao dao;

    protected Map<Key, Object> valueCache = new HashMap<Key, Object>();

    public EasemobModel(Context ctx) {
        context = ctx;
    }

    public boolean saveContactList(List<EaseUser> contactList) {
        dao = ((PaoPaoApplication) context.getApplicationContext()).getDaoSession().getUserInfoDao();
        return true;
    }

    public Map<String, EaseUser> getContactList() {
        dao = ((PaoPaoApplication) context.getApplicationContext()).getDaoSession().getUserInfoDao();
        return null;
    }

    public void saveContact(EaseUser user) {
        dao = ((PaoPaoApplication) context.getApplicationContext()).getDaoSession().getUserInfoDao();

    }

    /**
     * 设置当前用户名（没用）
     *
     * @param username
     */
    public void setCurrentUserName(String username) {
        SpUtil.setLoginUserName(context, username);
    }

    /**
     * 获取当前用户名，由系统服务器提供
     *
     * @return
     */
    public String getCurrentUsernName() {
        return SpUtil.getLoginUserName(context);
    }

    //接受新消息的通知
    public void setSettingMsgNotification(boolean paramBoolean) {
        SpUtil.setSettingMsgNotification(context, paramBoolean);
        valueCache.put(Key.VibrateAndPlayToneOn, paramBoolean);
    }

    public boolean getSettingMsgNotification() {
        Object val = valueCache.get(Key.VibrateAndPlayToneOn);

        if (val == null) {
            val = SpUtil.getSettingMsgNotification(context);
            valueCache.put(Key.VibrateAndPlayToneOn, val);
        }

        return (Boolean) (val != null ? val : true);
    }

    //设置消息的声音
    public void setSettingMsgSound(boolean paramBoolean) {
        SpUtil.setSettingMsgSound(context, paramBoolean);
        valueCache.put(Key.PlayToneOn, paramBoolean);
    }

    public boolean getSettingMsgSound() {
        Object val = valueCache.get(Key.PlayToneOn);

        if (val == null) {
            val = SpUtil.getSettingMsgSound(context);
            valueCache.put(Key.PlayToneOn, val);
        }

        return (Boolean) (val != null ? val : true);
    }

    //设置消息的震动
    public void setSettingMsgVibrate(boolean paramBoolean) {
        SpUtil.setSettingMsgVibrate(context, paramBoolean);
        valueCache.put(Key.VibrateOn, paramBoolean);
    }

    public boolean getSettingMsgVibrate() {
        Object val = valueCache.get(Key.VibrateOn);

        if (val == null) {
            val = SpUtil.getSettingMsgVibrate(context);
            valueCache.put(Key.VibrateOn, val);
        }

        return (Boolean) (val != null ? val : true);
    }

    //设置使用扬声器播放语音
    public void setSettingMsgSpeaker(boolean paramBoolean) {
        SpUtil.setSettingMsgSpeaker(context, paramBoolean);
        valueCache.put(Key.SpakerOn, paramBoolean);
    }

    public boolean getSettingMsgSpeaker() {
        Object val = valueCache.get(Key.SpakerOn);

        if (val == null) {
            val = SpUtil.getSettingMsgSpeaker(context);
            valueCache.put(Key.SpakerOn, val);
        }

        return (Boolean) (val != null ? val : true);
    }

    //设置群组黑名单
    public void setDisabledGroups(List<String> groups) {
        if (dao == null) {
            dao = ((PaoPaoApplication) context.getApplicationContext()).getDaoSession().getUserInfoDao();
        }

        List<String> list = new ArrayList<String>();
        list.addAll(groups);
        for (int i = 0; i < list.size(); i++) {
            if (EaseAtMessageHelper.get().getAtMeGroups().contains(list.get(i))) {
                list.remove(i);
                i--;
            }
        }

        valueCache.put(Key.DisabledGroups, list);
    }

    public List<String> getDisabledGroups() {
        Object val = valueCache.get(Key.DisabledGroups);

        if (dao == null) {
            dao = ((PaoPaoApplication) context.getApplicationContext()).getDaoSession().getUserInfoDao();
        }

        if (val == null) {
            valueCache.put(Key.DisabledGroups, val);
        }

        return (List<String>) val;
    }

    //设置用户黑名单
    public void setDisabledIds(List<String> ids) {
        if (dao == null) {
            dao = ((PaoPaoApplication) context.getApplicationContext()).getDaoSession().getUserInfoDao();
        }

        valueCache.put(Key.DisabledIds, ids);
    }

    public List<String> getDisabledIds() {
        Object val = valueCache.get(Key.DisabledIds);

        if (dao == null) {
            dao = ((PaoPaoApplication) context.getApplicationContext()).getDaoSession().getUserInfoDao();
        }

        if (val == null) {
            valueCache.put(Key.DisabledIds, val);
        }

        //noinspection unchecked
        return (List<String>) val;
    }

    //设置是否做了群组同步
    public void setGroupsSynced(boolean synced) {
        SpUtil.setGroupSynced(context, synced);
    }

    public boolean isGroupsSynced() {
        return SpUtil.getGroupSynced(context);
    }

    //允许群聊owner离开
    public void allowChatroomOwnerLeave(boolean value) {
        SpUtil.setallowChatroomOwnerLeave(context, value);
    }

    public boolean isChatroomOwnerLeaveAllowed() {
        return SpUtil.getallowChatroomOwnerLeave(context);
    }

    //离开群组是否删除全部信息
    public void setDeleteMessagesAsExitGroup(boolean value) {
        SpUtil.setDeleteMessagesAsExitGrou(context, value);
    }

    public boolean isDeleteMessagesAsExitGroup() {
        return SpUtil.getDeleteMessagesAsExitGrou(context);
    }

    //是否接受自动接受群组邀请
    public void setAutoAcceptGroupInvitation(boolean value) {
        SpUtil.setAutoAcceptGroupInvitation(context, value);
    }

    public boolean isAutoAcceptGroupInvitation() {
        return SpUtil.getAutoAcceptGroupInvitation(context);
    }

    //视频自适应码率编码
    public void setAdaptiveVideoEncode(boolean value) {
        SpUtil.setAdaptiveVideoEncod(context, value);
    }

    public boolean isAdaptiveVideoEncode() {
        return SpUtil.getAdaptiveVideoEncod(context);
    }




    enum Key {
        VibrateAndPlayToneOn,
        VibrateOn,
        PlayToneOn,
        SpakerOn,
        DisabledGroups,
        DisabledIds
    }
}
